<script setup lang="ts">
import {nextTick, ref, watch} from "vue";
import {AllContentsDto} from "@/type/allContents";
import {allContentsApi} from "@/api/allContents";
import Vditor from 'vditor';
import 'vditor/dist/index.css'

const props = defineProps({
  selectTitleId: {
    type: [Number, null],
    required: true
  },
  chatLoading: {
    type: Boolean,
    required: true
  }
});

const allContents = ref<AllContentsDto[]>([])

watch(allContents, async () => {
  await nextTick()
  for (const valueElement of allContents.value) {
    const $refElement: HTMLDivElement | null = document.querySelector(`#vditorRef${valueElement.id}`);
    if ($refElement) {
      // @ts-ignore
      await new Vditor.preview($refElement, valueElement.content, {});
    }
  }
  scrollToBottom()
}, {
  immediate: true,
  deep: true
})

const selectTitleFunc = () => {
  if (!props.selectTitleId) {
    return
  }
  allContentsApi.selectAll({titleId: props.selectTitleId}).then(res => {
    allContents.value = res
  })
}

watch(() => props.selectTitleId, (newv) => {
  console.log('watch(() => props.selectTitleId')
  allContents.value = []
  selectTitleFunc()
}, {
  immediate: true
})

const chatAddContentFunc = (obj: { role: string, content: string }) => {
  const allContentsDto = new AllContentsDto();
  allContentsDto.role = obj.role
  allContentsDto.content = obj.content
  allContents.value.push(allContentsDto)
}

const scrollToBottom = () => {
  nextTick(() => {
    const parent = document.getElementById('awkjndakwjndawdnakdkn444');
    if (!parent) {
      return
    }
    const total = getTotalHeightWithMargins(parent);
    document.querySelector('#awkjndakwjndawdnakdkn444')!.scrollTo({
      left: 0,
      top: total + 100,
    })
  })
}

function getTotalHeightWithMargins(parentElement: HTMLElement) {
  const children = parentElement.children;
  let totalHeight = 0;

  Array.from(children).forEach(child => {
    const style = window.getComputedStyle(child);
    const marginTop = parseFloat(style.marginTop);
    const marginBottom = parseFloat(style.marginBottom);
    const height = parseFloat(style.height);

    totalHeight += marginTop + height + marginBottom;
  });

  return totalHeight;
}

defineExpose({
  chatAddContentFunc
})
</script>

<template>
  <div class="el-contents" id="awkjndakwjndawdnakdkn444">
    <div
      v-for="item in allContents"
      :key="item.id"
      :class="item.role==='user'?'right':'left'"
    >
      <div v-if="item.role!=='user'" :id="'vditorRef'+item.id">
        {{ item.content }}
      </div>
      <div v-if="item.role==='user'">{{ item.content }}</div>
    </div>
    <div v-if="props.chatLoading" class="center">
      <div>思考中...</div>
    </div>
  </div>
</template>

<style scoped>

.el-contents {
  display: flex;
  flex-direction: column;
  gap: 16px;
  overflow: auto;
  padding: 16px 20px;
  font-family: 'Helvetica Neue', Helvetica, 'PingFang SC', 'Hiragino Sans GB', Arial, sans-serif;
  height: 100%;
  box-sizing: border-box;
}

.el-contents > * {
  max-width: 80%;
  padding: 12px 16px;
  border-radius: 8px;
  font-size: 14px;
  line-height: 1.5;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
  word-break: break-word;
}

.el-contents .left {
  background-color: #ffffff;
  border: 1px solid #ebeef5;
  border-bottom-left-radius: 0;
  color: #606266;
  align-self: flex-start;
}

.el-contents .right {
  background-color: #409eff;
  color: #ffffff;
  border-bottom-right-radius: 0;
  align-self: flex-end;
}

.el-contents .center {
  background-color: transparent;
  width: 100%;
  padding: 8px 0;
  text-align: center;
  color: #909399;
  font-size: 13px;
}

.el-contents .center > * {
  margin: 0 auto;
  padding: 8px 16px;
  background-color: #f5f7fa;
  border-radius: 16px;
  display: inline-block;
}
</style>